// pages/AddGame/AddGame.js
const db = wx.cloud.database()
const app = getApp()
let refreshTime = 0

let bgArr = []

let search = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    bgArr = []
    db.collection('bgDB').limit(15).get({
      success: function(res) {
        console.log(res.data)
        for (let i = 0 ; i < res.data.length ; i++) {
          let bg = {
            name: res.data[i].name,
            pic: res.data[i].pic,
            time: res.data[i].time,
            difficulty: res.data[i].difficulty,
            players: res.data[i].players,
            id: res.data[i]._id,
            brief: res.data[i].brief,
            types:res.data[i].types
          }
          bgArr.push(bg)
        }
        that.setData({bgArr})
      }
    })
    db.collection('userDB').where({
        _openid: app.globalData.openid
    }).get({
        success: function(res) {
            that.setData({
                userID: res.data[0]._id
            })
        }
    })
  },
  cancel() {
      this.setData({selected:false})
  },
  previewInfo(e) {
    let index = e.currentTarget.dataset.index
    console.log(bgArr[index]);
    this.setData({
        pic: bgArr[index].pic,
        name: bgArr[index].name,
        time: bgArr[index].time,
        difficulty: bgArr[index].difficulty,
        players: bgArr[index].players,
        id: bgArr[index].id,
        selected: true,
        brief: bgArr[index].brief,
        types:bgArr[index].types
    })
  },
  addGame(e) {
      let that = this
      let id = this.data.id
      wx.showLoading({
        title: '添加中...',
      })
      db.collection("userDB").doc(that.data.userID).update({
        data: {
            [`games.${id}`]: db.serverDate()
        },
        success: (res) => {
            wx.showToast({
                title: '添加成功！',
                icon: 'success'
            })
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },
  getText(e) {
    search = e.detail.value;
    console.log(search);
  },
  searchGame(e) {
    let that = this
    bgArr = []
    console.log(search);
    db.collection('bgDB').where({
      name: db.RegExp({
        regexp: search + '+',
        options: 'i',
      })
    }).get({  
      success: function(res) {
      console.log(res.data)
      for (let i = 0 ; i < res.data.length ; i++) {
        let bg = {
          name: res.data[i].name,
          pic: res.data[i].pic, 
          time: res.data[i].time,
          difficulty: res.data[i].difficulty,
          players: res.data[i].players,
          id: res.data[i]._id,
          brief:res.data[i].brief,
          types:res.data[i].types
        }
        bgArr.push(bg)
      }
      console.log(bgArr);
      that.setData({bgArr})
    }})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this
    db.collection('bgDB').limit(6).skip(15+refreshTime*6).get({
      success: function(res) {
        console.log(res.data)
        for (let i = 0 ; i < res.data.length ; i++) {
          let bg = {
            name: res.data[i].name,
            pic: res.data[i].pic,
            time: res.data[i].time,
            difficulty: res.data[i].difficulty,
            players: res.data[i].players,
            types:res.data[i].types,
            brief:res.data[i].brief
          }
          bgArr.push(bg)
        }
        that.setData({bgArr})
      }
    })
    refreshTime += 1
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})